<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <product-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="datasource" :selection.sync="selection">
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit()" v-any-role="['company','super_admin','admin']">
            新建
          </el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatch">
            删除
          </el-button>
        </template>

        <!-- 图片列 -->
        <template v-slot:thumb="{ row }">
          <img :src="row.thumb" style="width: 50px; height: 50px; " />
        </template>
        <!-- price-->
        <template v-slot:price="{ row }">
          <span v-if="row.price">{{ row.price }} 元</span>
        </template>
        <!-- 状态列 -->
        <template v-slot:statusFlag="{ row }">
          <el-switch :active-value="0" :inactive-value="1" v-model="row.statusFlag" @change="editStatus(row)" />
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)" v-any-role="['company','super_admin','admin']">
            修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此用户吗？" @confirm="remove(row)">
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
import ProductSearch from './components/product-search.vue';
import {
  pageProducts,
  removeProduct,
  removeProducts,
  updateProductStatus
} from '@/api/product';

export default {
  name: 'ProductIndex',
  components: {
    ProductSearch
  },
  data() {
    return {
      // 表格列配置  
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          align: 'center'
        },
        {
          slot: 'thumb',
          prop: 'thumb',
          label: '图片',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'title',
          label: '标题',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          slot: 'price',
          prop: 'price',
          label: '价格',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'createTime',
          align: 'center',
          label: '创建时间',
          showOverflowTooltip: true,
        },
        {
          slot: 'statusFlag',
          prop: 'statusFlag',
          label: '状态',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          columnKey: 'action',
          label: '操作',
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  created() {
    // 如果是管理员
    if (this.$hasAnyRole(['admin', 'super_admin'])) {
      // 把企业名称添加到table的第二列
      this.columns.splice(1, 0, {
        prop: 'companyName',
        label: '企业名称',
        align: 'center',
        showOverflowTooltip: true
      });
    }
  },
  methods: {
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return pageProducts({ ...where, ...order, page, limit });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      // this.current = row;
      // 跳转到编辑页面
      this.$router.push({
        path: "./edit",
        query: { id: row ? row.id : '' },
      });
    },
    /* 打开导入弹窗 */
    openImport() {
      this.showImport = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      removeProduct(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的产品吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          removeProducts(this.selection.map((d) => d.ProductId))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      updateProductStatus(row.id, row.statusFlag)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
        })
        .catch((e) => {
          loading.close();
          row.statusFlag = !row.statusFlag ? 1 : 0;
          this.$message.error(e.message);
        });
    }
  }
};
</script>
